package manage.action;

import m.common.action.ActionMeta;
import m.common.action.VueResult;
import m.common.model.util.ModelQueryList;
import m.common.model.util.QueryCondition;
import m.html.VueListResult;
import m.html.element.attr.Align;
import m.html.element.attr.ButtonType;
import m.html.element.other.Param;
import m.html.list.*;
import m.system.util.JSONMessage;
import m.system.util.StringUtil;
import manage.model.AdminGroup;
import manage.model.AdminGroupLink;
import manage.service.AdminGroupLinkService;

@ActionMeta(name="manage/adminGroupLink")
public class AdminGroupLinkAction extends ManageAction {
	public AdminGroupLink model;
	
	public String adminGroupOid;
	public String[] adminOids;
	
	public JSONMessage setLink() throws Exception {
		verifyAdminOperPower("manage_system_power");
		getService(AdminGroupLinkService.class).setLink(adminGroupOid,adminOids);
		return new JSONMessage(0,"保存成功");
	}
	public JSONMessage getLink() throws Exception {
		verifyAdminOperPower("manage_system_power");
		return new JSONMessage(0)
				.push("allLink", getService(AdminGroupLinkService.class).getLink(adminGroupOid));
	}

	/**
	 * 机构用户列表
	 * 
	 *  Exception
	 */
	public VueResult orgUserList() throws Exception {
		verifyMenu("A1104");
		boolean power = hasAdminOperPower("manage_system_power");
		AdminGroup org = new AdminGroup();
		if (!StringUtil.isSpace(this.getParamsValue("deptOid"))) {
			org = ModelQueryList.getModel(AdminGroup.class, this.getParamsValue("deptOid"), "*");
		} else {
			org.setNum("");
			org.setName("全部");
		}
		VueListResult result = getVueListResult(org.getNum() + " " + org.getName(), AdminGroupLink.class, actionUrl("orgUserList"));
		result.setSubTitle("用户列表");
		result.setHeight(-15);
		result.setPageSize(20);
		result.addCol(toIndexColumn());
		result.addCol(toTextColumn("所属部门", "adminGroup.oid",130, true).setSort(true)
			.setDictType(AdminGroup.class, "C;"+StringUtil.noSpace(org.getOid())));
		if(power) result.addCol(toLinkColumn("用户名", "admin.username",
			new ListEvent("page/base/viewGroupMenuPage.js", EventType.POPOVER,800)
				.addParam("adminOid","admin.oid")
		).setSort(true));
		result.addCol(toTextColumn("用户编号", "admin.userNo",true));
		result.addCol(toTextColumn("真实姓名", "admin.realname", true));
		result.addCol(toTextColumn("管理员组", "admin.adminGroup.oid", true).setDictType(AdminGroup.class, "A"));
		result.addCol(toDateTimeColumn("最后登录时间", "admin.lastLoginTime",140, true).setSort(true));
		result.addCol(toNumberColumn("登录次数", "admin.loginCount").setNumberFormat("#,##0"));
		result.addCol(toStatusColumn(power, "admin.status", "manage/adminLogin").setFilter(true));
		if (power) result.addCol(toSpaceColumn("操作",
			ListButton.modal("修改", ButtonType.PRIMARY, actionUrl(AdminLoginAction.class,"toEdit"), 600, SuccessMethod.REFRESH)
				.addParam("model.oid", "admin.oid")
		).setFixed(Align.RIGHT));
		if (power && !StringUtil.isSpace(org.getOid()) && !StringUtil.isSpace(org.getParent().getOid())) {
			result.addButton(
				ListButton.modal("添加", ButtonType.PRIMARY, actionUrl(AdminLoginAction.class,"toEdit"), 600, SuccessMethod.REFRESH)
					.addQueryParam(Param.value("link.adminGroup.oid", org.getOid()))
					.addQueryParam(Param.value("link.type", "C"))
			);
			result.addButton(
				ListButton.modal("关联用户", ButtonType.PRIMARY, "page/base/setAdminGroupLinkPage.js", 850, SuccessMethod.REFRESH)
					.addQueryParam(Param.value("adminGroupOid", org.getOid()))
			);
			result.addButton(
				ListDropdown.def("权限").add(
					ListButton.modal("菜单设置", ButtonType.DEFAULT, "page/base/setGroupMenuPage.js", 700)
						.addQueryParam(Param.value("groupOid", org.getOid())),
					ListButton.modal("权限设置", ButtonType.DEFAULT, "page/base/setAdminGroupPowerPage.js", 700)
						.addQueryParam(Param.value("groupOid", org.getOid()))
				)
			);
		}
		String deptOid=this.getParamsValue("deptOid");
		if(!StringUtil.isSpace(deptOid)){
			result.addCondition(QueryCondition.or(
				QueryCondition.eq("adminGroup.oid", deptOid),
				QueryCondition.eq("adminGroup.org.oid", deptOid),
				QueryCondition.eq("adminGroup.dept.oid", deptOid)
			));
		}
		result.addCondition(QueryCondition.eq("type", "C"));
		return result;
	}
	public AdminGroupLink getModel() {
		return model;
	}
	public void setModel(AdminGroupLink model) {
		this.model = model;
	}

	public String getAdminGroupOid() {
		return adminGroupOid;
	}

	public void setAdminGroupOid(String adminGroupOid) {
		this.adminGroupOid = adminGroupOid;
	}

	public String[] getAdminOids() {
		return adminOids;
	}

	public void setAdminOids(String[] adminOids) {
		this.adminOids = adminOids;
	}

}
